//
//  TKYTitleTextField.m
//  填报进度质量
//
//  Created by apple on 2018/7/25.
//  Copyright © 2018年 铁科院. All rights reserved.
//

#import "TKYTitleTextField.h"
#import <Masonry.h>
@interface TKYTitleTextField()<UITextFieldDelegate>


@property (nonatomic, strong) UIView *bottomLineView;


@end
@implementation TKYTitleTextField

- (instancetype)initWithFrame:(CGRect)frame titleLabelText:(NSString *)titleLabelText placeholderText:(NSString *)placeholderText {
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.titleLabel];
        self.titleLabel.text = titleLabelText;
        
        [self addSubview:self.textField];
        self.textField.delegate = self;
        self.textField.placeholder = placeholderText;
        //cself.textField.backgroundColor = [UIColor redColor];
        
        [self addSubview:self.selectedButton];
        [self addSubview:self.bottomLineView];
        [self addSubview:self.imageV];
        
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(kWidthScale*10);
            make.top.equalTo(self).with.offset(0);;
            make.bottom.equalTo(self).with.offset(kHeightScale *0);
            make.width.mas_equalTo(kWidthScale*80);
        }];
        
        [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.titleLabel.mas_right).with.offset(kWidthScale*0);
            make.right.equalTo(self).with.offset(-kWidthScale*35);
            make.top.equalTo(self.titleLabel.mas_top);
            make.bottom.equalTo(self).with.offset(kHeightScale *0);
                //make.width.mas_equalTo(kWidthScale*100);
        }];
        
        [self.selectedButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.titleLabel.mas_right).with.offset(kWidthScale*10);
            make.right.equalTo(self).with.offset(-kWidthScale*25);
            make.top.equalTo(self.titleLabel.mas_top);
            make.bottom.equalTo(self).with.offset(-kHeightScale *5);
            //make.width.mas_equalTo(kWidthScale*100);
        }];
        
        [self.imageV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).with.offset(-kWidthScale*20);
            make.top.equalTo(self).with.offset(15);
            make.height.mas_equalTo(kWidthScale*10);
            make.width.mas_equalTo(kWidthScale*10);
        }];
        
        [self.bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).with.offset(kWidthScale*10);
        make.right.equalTo(self).with.offset(-kWidthScale*10);
        make.bottom.equalTo(self).with.offset(-kHeightScale *1);
            make.height.mas_equalTo(kWidthScale*1);
        }];
    }
    return self;
}

- (UILabel *)titleLabel{
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.text = @"任务标题";
        _titleLabel.font = Font(12);
    }
    return _titleLabel;
}

- (UITextField *)textField{
    if (_textField == nil) {
        _textField = [[UITextField alloc]init];
        _textField.font = Font(12);
    
        _textField.textAlignment = NSTextAlignmentRight;
        //_textField.text = @"13243";
    }
    return _textField;
}

- (UIView *)bottomLineView{
    if (_bottomLineView == nil) {
        _bottomLineView = [[UIView alloc]init];
        _bottomLineView.backgroundColor = kMainColour;
    }
    return _bottomLineView;
}

- (UIButton *)selectedButton{
    if (_selectedButton == nil) {
        _selectedButton = [UIButton buttonWithType:UIButtonTypeCustom];
      //  [_selectedButton setTitle:@"请选择" forState:UIControlStateNormal];
        _selectedButton.titleLabel.textAlignment = NSTextAlignmentRight;
        [_selectedButton addTarget:self action:@selector(selectedButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        _selectedButton.backgroundColor = [UIColor clearColor];
    }
    return _selectedButton;
}

- (UIImageView *)imageV{
    if (_imageV == nil) {
        _imageV = [[UIImageView alloc]init];
        _imageV.image = [UIImage imageNamed:@"select_btn"];
    }
    return _imageV;
}

- (void)selectedButtonAction:(UIButton *)button{
    if (self.selectedButtonBlock) {
        self.selectedButtonBlock(button);
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (self.textFieldFBlock) {
        self.textFieldFBlock(textField.text);
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (self.textFieldFBlock) {
        self.textFieldFBlock(textField.text);
    }
    return YES;
}
@end
